#! /bin/bash


build_ccls()
{
	echo "install packages"
	dnf install -y clang-devel llvm-devel
	if [ -d ccls ]; then
		read -p "./ccls/ exitst, remove? <y/ctrl-c>?" ans
		if [ "$ans" == y ]; then
			rm -rf ccls
		fi
	fi
	git clone --depth=1 --recursive https://github.com/MaskRay/ccls
	pushd ccls
	cmake .
	make -j8
	if [ -e ccls ]; then
		cp ccls /opt/ccls
	fi
	popd

	echo "/opt/ccls"
	rm -rf ccls
}

if ! which dnf; then
	echo "not fedora distro, exit..."
	exit
fi

if [[ -e /opt/nvim-linux-$(uname -m).appimage  ]];then
	echo found /opt/nvim-linux-$(uname -m).appimage, skip
else
	wget -P /opt https://github.com/neovim/neovim/releases/download/v0.11.0/nvim-linux-$(uname -m).appimage
	chmod a+x /opt/nvim-linux-$(uname -m).appimage
fi

dnf install -y curl
dnf install -y ripgrep
dnf install -y fd-find
dnf install -y git
dnf install -y g++ gcc pip
dnf install -y python3-pip
dnf install -y cmake npm
pip install  pynvim

if npm list -g | grep -q "pyright@"; then
	echo "npm already installed pyright, skip"
else
	npm config set registry https://registry.npm.taobao.org
	# see https://blog.csdn.net/qq_49907632/article/details/135974644
	# pyright needs npm
	npm cache clean --force
	npm config set strict-ssl false
	npm install --global pyright
fi

if [[ ! -f /opt/ccls ]]; then
	echo "builing ccls"
	build_ccls
fi

[ ! -d ~/.config ]  && mkdir -p ~/.config
[ -e ~/.config/nvim -o -L ~/.config/nvim ] && rm -rf ~/.config/nvim

ln -s $PWD ~/.config/nvim
if command -v rg > /dev/null 2>&1 ; then
	:
else
	echo "rg not installed !!! "
	echo "wget https://github.com/BurntSushi/ripgrep/releases/download/14.1.1/ripgrep-14.1.1-x86_64-unknown-linux-musl.tar.gz"
fi

if command -v fd > /dev/null 2>&1 ; then
	echo "fd not installed !!!! "
else
	echo "wget https://github.com/sharkdp/fd/releases/download/v10.2.0/fd-v10.2.0-x86_64-unknown-linux-musl.tar.gz"
fi

/opt/nvim-linux-$(uname -m).appimage --headless "+Lazy! sync" +qa
